<?php
/**
 * Created by PhpStorm.
 * User: sxg
 * Date: 2023-07-16
 * Time: 12:02
 */

namespace App\Controller;

use Socket\Ms\Protocols\Mqtt;
use Socket\Ms\Protocols\MqttClient;
use Socket\Ms\Server;
use Socket\Ms\TcpConnections;

class WsBaseController {

    public $client;

    public function __construct(TcpConnections $connections) {
        $this->client = $connections;
    }

    public function publishEsp32Data($topic, $data): int {
        $frame = (new MqttClient())->responsePUBLISHPACK($topic, $data);
        //遍历客户端转发数据给芯片
        $flag = 0;
        foreach (Server::$_connections as $client) {
            /**@var $client TcpConnections*/
            if ($client->protocol instanceof Mqtt) {
                $client->sendDataForMqttPublish($frame);
                $flag = 1;
            }
        }
        return $flag;
    }
}